option VALIDVARNAME=UPCASE;
*%include "&studyroot\Work\Programs\macros\template\TDD_formats.sas";

proc format;
   invalue vspreci
      "BMI"=1
      "WEIGHT"=1
      "HEIGHT"=1
      "DIABP"=0
      "SYSBP"=0
      "PULSE"=0
      "HR"=0
      "RESP" =0
      "TEMP" =1  
      "OXYSAT"=0
      other   =.;

/*   value faperiod*/
/*      0="Screening" */
/*      1="Period 1"*/
/*      2="Period 2"*/
/*      3="Follow-Up";*/
quit;

%get_data(pre=adam,lib=adam,lst=adsl);

%mk_param(vs,in=vs,out=sdtmvs,ord=  VSTESTCD  ,lib=sdtm,fmt=3,debug=0,dropnd=0,use_cat_for_unit=0 );
%*get_supp(datain=vs);

proc sort data=_CODED_SDTMvs out=_CODED_SDTMvs;
by usubjid;
where studyid ne " ";
run;

data vs_adsl;
  merge _CODED_SDTMvs(in=a) adam.adsl;
  by usubjid;
  if a;
  if upcase(VSTESTCD) = "VSALL" then do;PARAMN= 99; PARAMCD = vstestcd; end;
run;

proc freq data =vs_adsl ;
   tables visit*visitnum paramn*paramcd*vstestcd/ list missing nocum nopercent;
run;

data all1;
  format ADT yymmdd10. ATM time5. ADTM DATETIME13.;
  length AVISIT /*ATPT*//* PARAM*/  AVALC /*ANRLO ANRHI*/ visityp $50.;
  set vs_adsl;
  by usubjid;
/*  if vsstat ="NOT DONE" then do; vsstresn=.; vsstresc = ""; vsorres=""; end;*/
  if length(vsdtc)> 10 then vsdtn = input(vsdtc,is8601DT.);
  else if vsdtc ne "" then vsdtn = input(vsdtc, yymmdd10.);

  SRCSEQ=vsseq;
  SRCDOM=strip(upcase(domain));

  *VISITYP;

/*  if visitnum^=int(visitnum) then visityp="Unscheduled";*/
/*  else visityp="Scheduled";*/
  If index(visit,"Unsch") then VISITYP="Unscheduled";
  else VISITYP="Scheduled";


  *AVISITN;
  if visitnum ne . then AVISITN=int(visitnum);
   *AVISIT;
  if avisitn ne . then AVISIT = strip(put(avisitn,visitn.));

  *avisit=put(avisitn, visitn.);

 if avisit="Early Term" then avisit="Early Term";
 else avisit=avisit;

  *ATPT;
  *ATPT=strip(vstpt);
  *ATPTN;
  *if vstptnum ne . then ATPTN=vstptnum; 
 
  *AVAL;
  if vsstresn ne . then AVAL=vsstresn;
  
  *VSPREC;
  VSPREC=input(paramcd,vspreci.);

  *AVALC;
  fmt = "8." || strip(put(VSPREC, best.));
  AVALC=strip(putn(vsstresn,fmt));

  *ADT/ATM;
  if ^missing(vsdtc) then do;
    *ADT=input(substr(vsdtc,1,10),yymmdd10.);
	 ADT=input(substr(vsdtc,1,10),is8601da.);
    if length(vsdtc)>10 then do; ATM=input(substr(vsdtc,12,5),time5.); ADTM=input(vsdtc, is8601DT.); end;
    ADY=vsdy;
   end;


/*IF LENGTH (VSDTC)=10 THEN  ADTM1=INPUT(STRIP(VSDTC)||'T00:00:00', E8601DT.);*/
/*ELSE ADTM1=ADTM;*/
IF ADTM >= TR01SDTM THEN DO; TRTP=TRT01P; TRTPN=TRT01PN;  TRTA=TRT01A; TRTAN=TRT01AN;* APERIOD=TRT01AN; END;
IF ADTM >= TR02SDTM THEN DO; TRTP=TRT02P; TRTPN=TRT02PN;  TRTA=TRT02A; TRTAN=TRT02AN; *APERIOD=TRT02AN; END;
IF ADTM >= TR03SDTM THEN DO; TRTP=TRT03P; TRTPN=TRT03PN;  TRTA=TRT03A; TRTAN=TRT03AN; *APERIOD=TRT03AN; END;
IF ADTM >= TR04SDTM THEN DO; TRTP=TRT04P; TRTPN=TRT04PN;  TRTA=TRT04A; TRTAN=TRT04AN;* APERIOD=TRT04AN; END;
IF ADTM >= TR05SDTM THEN DO; TRTP=TRT05P; TRTPN=TRT05PN;  TRTA=TRT05A; TRTAN=TRT05AN; *APERIOD=TRT05AN; END;
IF ADTM >= TR06SDTM THEN DO; TRTP=TRT06P; TRTPN=TRT06PN;  TRTA=TRT06A; TRTAN=TRT06AN;* APERIOD=TRT06AN; END;
 IF avisitn in(-2,-1,999) then do; TRTP=" "; TRTPN=.; TRTA=""; TRTAN=.;/*APERIOD=.;*/ end;
 *else if avisitn not in (-2,-1,999) then  aperiod=input(strip(scan(avisit, 2, '')), best.);
*if aperiod ne . then aperiodc="Period 0"||strip(put(aperiod,best.));
*else aperiodc=" ";

run;

proc freq data=all1;
	tables avisitn*avisit*visitnum*visit / list missing;
run;

***create all1;

Proc sort data =all1 out =sall ;
   by USUBJID PARAMN PARAMCD AVISITN VSDTC;
run;

data tofl miss;
   set sall;
   if avalc ne "" then output tofl;
  else output miss;
run;


data prior  post;
set tofl;
if ADT>TR01SDT>. or ADTm>TR01SDTm>. then output post;
else output prior;
run;

Proc sort data = prior ;
   by USUBJID PARAMN PARAMCD AVISITN  VSDTC;
run;

proc freq data=prior;
	tables avisit*visit / list;
run;

data _toblfl;
   length ANL01FL $1.;
   set prior;
   by USUBJID PARAMN PARAMCD AVISITN  VSDTC;
   if last.AVISITN then ANL01FL = 'Y';
run;

Proc sort data = post ;
   by USUBJID PARAMN PARAMCD AVISITN VSDTC;
run;

proc freq data=post;
	tables avisit*visit / list;
run;

data toblfl_;
   length ANL01FL $1.;
   set post;
   by USUBJID PARAMN PARAMCD AVISITN  VSDTC;
   if last.AVISITN then ANL01FL = 'Y';
run;

data tblfl;
   set _toblfl  toblfl_;
   by USUBJID ;  
/*   if avisitn <= 0 then do; if last.AVISITN; end;*/
/*else if avisitn > 0 then do; if first.AVISITN; end; */

/*   if visityp=:"U" then ANL01FL=" ";*/
/*   else if avalc=" " then ANL01FL=" ";*/
/*   else ANL01FL="Y";*/
run;

Proc freq data = tblfl;
   tables avisitn*avisit*visityp*visitnum*anl01fl/*vsstat*// list missing nocum nopercent;
run;

data info;
   set tblfl miss;* all;
   by USUBJID;
run;
proc sort data=info;
by usubjid paramn;
run;

data base(keep=usubjid param paramn paramcd aval avalc rename=(aval=base avalc=basec));
set info;
if  vsblfl eq 'Y';
proc sort;
by usubjid paramn;
run;
data final1;
	merge base(in=a) info(in=b);
	by usubjid paramn;
	if  b;
if avisitn not in  (-2,-1) then do;
	if base ne . and aval ne . then CHG=AVAL-BASE;
	else chg=.;
    *PCHG= ((AVAL-BASE)/BASE)*100;
	end;

run;




data final;
  merge adam.adsl final1(in=aa );
  by USUBJID;
  if aa;
  label STUDYID="Study Identifier"
        USUBJID="Unique Subject Identifier"
        SUBJID="Subject Identifier for the Study"
        SITEID="Study Site Identifier"
/*        LSUBJID="Subject (Sex/Age)"*/
        AVISIT="Analysis Visit"
        AVISITN="Analysis Visit (N)"
        VISITYP="Visit Type"
        ADT="Analysis Date"
        ATM="Analysis Time"
        ADTM="Analysis Datetime"
        ADY="Analysis Relative Day"
/*        APERIOD="Period"*/
/*        APERIODC="Period (C)"*/
        PARAM="Parameter"
        PARAMCD="Parameter Code"
        PARAMN="Parameter (N)"
        AVAL="Analysis Value"
        AVALC="Analysis Value (C)"
        BASE="Baseline Value"
        BASEC="Baseline Value (C)"
        CHG="Change from Baseline"
/*        CHGC="Change from Baseline (C)"*/
/*        PCHG="Percent Change from Baseline"*/
/*        PCHGC="Percent Change from Baseline (C)"*/

        ABLFL="Baseline Record Flag"

        ANL01FL="Analysis Record Flag 01"
    
        TRTP="Planned Treatment"
        TRTPN="Planned Treatment (N)"
        TRTA="Actual Treatment"
        TRTAN="Actual Treatment (N)"
        SRCSEQ="Source Sequence Number"
	    SRCDOM="Source Data"
	    
	
;
  
ABLFL=strip(vsblfl);

run;


Proc SQL;
   create table adamw.advs(label = "Vital Signs Analysis Dataset" sortedby =STUDYID USUBJID AVISITN PARAMN compress=yes) as
   SELECT STUDYID,USUBJID,SUBJID,SITEID,ARMCD,ACTARMCD,
          ENRLFL,SAFFL,RANDAFL,RANDBFL,COMPLFL,PKFL,PDFL,TRTSDT,TRTSDTM,TRTEDT,TRTEDTM,
          TRTP,TRTPN,TRTA,TRTAN,/*,APERIOD,*//*APERIODC,*/AVISIT,AVISITN,VISITYP,ADT,ATM,ADTM,ADY,
		  PARAM,PARAMN,PARAMCD,AVAL,AVALC,BASE,BASEC,CHG,ABLFL,
          ANL01FL,/*VSSTAT,*/VSCLSIG,TIMESPOS,
          SRCDOM,SRCSEQ
   FROM final
   order STUDYID, USUBJID, PARAMCD, PARAMN, AVISITN, ADTM 
   ;
QUIT;
proc contents data=adam.advs ;
run;
%clrw;
